package com.wifi.wifi2.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author ZhangYue26
 * @date 2020-07-13 10:44
 * @Description: 密码字典生成工具
 */
public class DictionaryUtils {

    private static List<String> zeroList = new ArrayList<>();
    static {
        zeroList.add("");
        zeroList.add("0");
        zeroList.add("00");
        zeroList.add("000");
        zeroList.add("0000");
        zeroList.add("00000");
        zeroList.add("000000");
        zeroList.add("0000000");
        zeroList.add("00000000");
        zeroList.add("000000000");
        zeroList.add("0000000000");
        zeroList.add("00000000000");
        zeroList.add("000000000000");
        zeroList.add("0000000000000");
        zeroList.add("00000000000000");
        zeroList.add("000000000000000");
        zeroList.add("0000000000000000");
        zeroList.add("00000000000000000");
    }
    

    /**
     * @Author ZhangYue26
     * @Description: 生成纯数字密码字典
     * @Description //TODO
     * @Date 2020/7/13 10:57
     * @Param [minLength, maxLength],生成最小长度与最大长度纯数字
     * @return String 返回自定路径
     **/
    public static String generateNumbersFile(int minLength,int maxLength,String dirPath) throws IOException {
        long useSpaceByte = 0;//生成字典时即将使用的字节数
        for (int len = minLength; len <= maxLength; len++) {
            useSpaceByte += (long) Math.pow(10, len)*(len+2);
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("生成字典将占用--"+useSpaceByte+"B--"+useSpaceByte/1024/1024+"MB--"+useSpaceByte/1024/1024/1024+"GB的空间");
        System.out.println("是否继续生成？输入“是”或“1”或“yes”或不输入表示继续生成");
        String isTrue = scanner.next();
        String fileName = "密码字典" + System.currentTimeMillis() + ".txt";
        File file = new File(dirPath+fileName);
        OutputStream outputStream = new FileOutputStream(file);
        
        if (isTrue.equals("") || isTrue.trim().equals("是") || isTrue.trim().toLowerCase().equals("yes") || isTrue.trim().equals("1")) {
            //继续生成
            System.out.println("字典生成中");
            for (int len = minLength; len <= maxLength; len++) {
                generateNumbersFile(len, outputStream);
            }
            System.out.println("字典生成成功");
        }
        return dirPath + fileName;

    }


    /**
     * @Author ZhangYue26
     * @Description: 生成长度为len的字典文件
     * @Description //TODO
     * @Date 2020/7/13 11:15
     * @Param [len, filePath]
     * @return void
     **/
    public static void generateNumbersFile(int len,OutputStream outputStream) throws IOException {
        long number = (long) Math.pow(10, len);
        for (long i = 0; i < number; i++) {
            String value = String.valueOf(i);
            if (len > value.length()) {
                value = zeroList.get(len-value.length()) + value;
            }
            outputStream.write((value+"\r\n").getBytes());
            outputStream.flush();
        }


    }

    public static void main(String[] args) throws IOException {
        generateNumbersFile(2,7,"D:\\");
    }

}
